﻿using log4net;
using log4net.Config;
using Microsoft.VisualBasic.Devices;
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;
using Topshelf;

namespace QuartzTest
{
    class Program
    {
        static readonly ILog Log = LogManager.GetLogger(typeof(Program));
        static void Main(string[] args)
        {
            string fileName = "log4net.config";
            try
            {
                ComputerInfo ci = new ComputerInfo();
                var logCfg = new FileInfo(AppDomain.CurrentDomain.BaseDirectory + fileName);
                XmlConfigurator.ConfigureAndWatch(logCfg);
                //每生成一次就更新一次版本号。从之前版本更新配置
                 
                var ass = System.Reflection.Assembly.GetExecutingAssembly().GetName();
                ThreadPool.GetMaxThreads(out int workerThreadsMx, out int portThreadsMx);
                ThreadPool.GetMinThreads(out int workerThreadsMin, out int completionPortThreadsMin);
                Log.Info($"Program start...Assembly version {ass.Version},Architecture={ass.ProcessorArchitecture},.Net FrameWork:{RuntimeInformation.FrameworkDescription}，Max workerThreads={workerThreadsMx}，Max portThreads={portThreadsMx}，Min workerThreads={workerThreadsMin}，Min portThreads={completionPortThreadsMin}");
                Log.Info("-- Environment members --");
                Log.Info($"  |-OSVersion:{Environment.OSVersion}");
                Log.Info($"  |-ProcessorCount:{Environment.ProcessorCount}");
                Log.Info($"  |-WorkingSet :{Environment.WorkingSet }({Environment.WorkingSet/1024/1024}M)");
                Log.Info("-- ComputerInfo members --");
                Log.Info($"  |-TotalPhysicalMemory(M):" + ci.TotalPhysicalMemory / 1024 / 1024);
                Log.Info($"  |-TotalVirtualMemory(M):" + ci.TotalVirtualMemory / 1024 / 1024);
                Log.Info($"  |-AvailablePhysicalMemory(M):" + ci.AvailablePhysicalMemory / 1024 / 1024);
                Log.Info($"  |-AvailableVirtualMemory(M):" + ci.AvailableVirtualMemory / 1024 / 1024);
                //Schedule.StartSchedule();
                HostFactory.Run(x =>
                {
                    x.Service<Cellserver>();
                    x.RunAsLocalSystem();                    
                    x.SetDescription("QuartzTest");
                    x.SetDisplayName("QuartzTest");
                    x.SetServiceName("QuartzTest");
                });
                Console.ReadLine();
            }
            catch (StackOverflowException ex)
            {
                EE.LogError("StackOverflowException：", ex, Log);
            }
            catch (Exception ex)
            {
                EE.LogError("Program start FAIL：", ex, Log);
            }
        }
    }
}
